(function() {

    var goods_id;


    function _is_collect_goods(collect) {
        var result = false;
        if (jSouper.indexOf(collect, goods_id) !== -1) {
            result = true;
        }
        App.set("$Cache.goods_details.is_collect", result);
    };

    App.set("$Event.goods_details.goods.add_to_cart", function(e) {
        var cart_item = {
            goods_id: goods_id,
            num: App.get("$Cache.goods_details.buy_number")
        };
        if (App.get("loginer")) {
            coAjax.post(appConfig.user.cart, cart_item, function(result) {
                App.set("user.cart_detail", result.result);
                myConfirm("成功添加商品到购物车，是否立即转到购物车查看？", function() {
                    Path.jump("/cart.html");
                });
            });
        } else {
            var cache_cart = LS.get("$Cache.cart");
            try {
                cache_cart = JSON.parse(cache_cart);
            } catch (e) {
                console.error(e)
            }
            if (!(cache_cart instanceof Array)) {
                cache_cart = [];
            }
            cache_cart.push(cart_item);
            myConfirm("您还未登陆，是否先去登陆？", function() {
                Path.jump("/sign_in.html?cb_url=" + encodeURIComponent(App.get("$Cache.top.cb_url")));
            });
        };
    });
    App.set("$Event.goods_details.goods.selectRecUrl", function(e) {
        //全选
        this.select && this.select();
    });
    eventManager.is(App.get("loginer.collect"), "getLoginer", function() {
        var collect = App.get("loginer.collect") || [];
        _is_collect_goods(collect);
    });

    App.set("$Event.goods_details.goods.collect_toggle", function() {
        if (!App.get("loginer")) {
            myConfirm("请先登陆后再收藏，是否立即前去登陆？", function() {
                Path.jump("/sign_in.html?cb_url=" + App.get("$Cache.top.cb_url"));
            });
            return;
        };
        if (App.get("$Cache.goods_details.is_collect")) {
            coAjax["delete"](appConfig.user.collect_remove, {
                goods_id: goods_id
            }, function(result) {
                alert("已经取消收藏");
                _is_collect_goods(result.result);
            }, function(errorCode, xhr, errorMsg) {
                alert("error", errorMsg)
            });
        } else {
            coAjax.post(appConfig.user.collect_add, {
                goods_id: goods_id
            }, function(result) {
                alert("success", "收藏成功");
                _is_collect_goods(result.result);
            }, function(errorCode, xhr, errorMsg) {
                alert("error", errorMsg)
            });
        }
    });

    // 显示微信信息
    App.set("$Event.goods_details.show_wechat_info", function() {
        App.model.toggle("$Cache.goods_details.show_wechat_info");
    });

    Path.on("/__basename__.html", function(_current_location) {
        var queryString = _current_location.query;
        // var recommender_id = queryString.get("rcid");
        // if (recommender_id) {
        //  //将推荐者的信息保存起来，或者直接覆盖上一个推荐者，以最后一个推荐者为准
        //  LS.set("recommender_id", recommender_id);
        // }
        // App.set("$Cache.recommender_id", recommender_id);
        // App.set("$Cache.recommender_url", Model.Observer(function() {
        //  var user_id = App.get("loginer._id");
        //  var result = "http://" + location.host + location.pathname + "?id=" + queryString.get("id") + "&rcid=" + user_id;
        //  return result;
        // }));
        goods_id = queryString.get("id");
        //参数错误，返回首页
        if (!goods_id) {
            myConfirm("找不到此商品", function() {
                Path.jump("/main.html");
                return;
            });
        };
        //记录访问量
        coAjax.post(appConfig.hot_add + goods_id, function(result) {
            App.set("$Cache.goods_details.hot_num", result.result);
        });
        // 获取商品微信信息
        $.getJSON("http://121.40.18.23:1518/api/complex/WechatClientService", {
            model_id: goods_id
        }, function(wechat_data) {
            console.log(wechat_data);
            App.set("$Cache.goods_details.wechat_data", wechat_data);
        });
        // 加载商品数据，绑定滑动事件
        require(["touch"], function(touch) {
            var goods_details_dtd = new $.Deferred();

            function init_loading_goods_info(dtd) {
                App.set("$Cache.goods_details.loading_completed", false);
                coAjax.get(appConfig.goods.detail + goods_id, function(result) {
                    var goods_info = result.result;
                    //处理数据格式
                    goods_info.price = +goods_info.price || 0;
                    goods_info.price_toFixed_2 = goods_info.price.toFixed(2);
                    goods_info.fare = +goods_info.fare || 0;
                    goods_info.fare_toFixed_2 = goods_info.fare.toFixed(2);
                    App.set("goods_detail", goods_info);
                    eventManager.fire("GoodsLoaded");
                    dtd.resolve(goods_info);
                });
                return dtd;
            };
            $.when(init_loading_goods_info(goods_details_dtd)).then(function(goods) {
                App.set("$Cache.goods_details.loading_completed", true);
                document.body.scrollTop = 0;
                var $movebox = $("#movebox");
                $movebox.css({
                    marginLeft: 0
                });
                var _len = $movebox.children().length;
                // 左划
                touch.on(".bigimg", "swipeleft", function(bigimg) {
                    if ((parseInt($(this).attr("data")) + 1) < _len) {
                        $movebox.stop().animate({
                            marginLeft: -$movebox.width() * (parseInt($(this).attr("data")) + 1)
                        }, 150);
                    };
                });
                // 右划
                touch.on("#movebox", "swiperight", function(movebox) {
                    if (parseInt($(this).attr("data")) > 0) {
                        $movebox.stop().animate({
                            marginLeft: -$movebox.width() * (parseInt($(this).attr("data")) - 1)
                        }, 150);
                    };
                });
            });
        });


        // //获取评价数据
        // coAjax.get(appConfig.user.can_eval + goods_id, function(result) {
        //  App.set("$Cache.goods_details.eval.can_evaluation_able", result.result.can_evaluation_able);
        // });

        //根据HASH指令自动添加到购物车
        if (_current_location.hash.indexOf("ADD_TO_CART") !== -1) {
            App.get("$Event.goods_details.goods.add_to_cart")();
        }

        // 分享
        // 微博
        App.set('$Event.goods_details.shareToWeibo', function() {
            var title = "我觉得#" + App.get("goods_detail.goods_name") + "#不错，跟大家分享一下";
            console.log(title);
            var pic = App.get("goods_detail.preview_img_url") + "?imageView/1/w/100/h/100";
            var rLink = "http://" + location.host.toString() + "/goods_details.html?id=" + App.get('goods_detail._id');
            var site = location.host;
            var summary = App.get('goods_detail.intro');
            window.open("http://service.weibo.com/share/share.php?url=" + encodeURIComponent(rLink) + "&title=" + encodeURIComponent(title.replace(/&nbsp;/g, " ").replace(/<br \/>/g, " ")) + "&pic=" + encodeURIComponent(pic),
                "分享至新浪微博",
                "toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no");
        });
        // QQ空间
        App.set('$Event.goods_details.shareQzone', function() {
            var title = "我觉得#" + App.get("goods_detail.goods_name") + "#不错，跟大家分享一下";
            console.log(title);
            var pic = App.get("goods_detail.preview_img_url") + "?imageView/1/w/100/h/100";
            var rLink = "http://" + location.host.toString() + "/goods_details.html?id=" + App.get('goods_detail._id');
            var site = location.host;
            var summary = App.get('goods_detail.intro');
            window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?title=' +
                encodeURIComponent(title) + '&url=' + encodeURIComponent(rLink) + '&summary=' +
                encodeURIComponent(summary) + '&pics=' + encodeURIComponent(pic) + '&site=' + encodeURIComponent(site), '_blank', 'scrollbars=no,status=no,resizable=yes');
        });
    });

    // 返回
    App.set("$Event.goods_details.detailGoBack", function() {
        if (App.get("$Cache.page_index") > 0) {
            window.history.back();
        } else {
            Path.jump("main.html");
        }
    });
    App.set("$Event.goods_details.show_share_link", function() {
        App.model.toggle("$Cache.show_share_link");
    });
}());
